package com.qianfeng.zhangwei.day30_bdmap.overlay;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.qianfeng.zhangwei.day30_bdmap.R;

import java.util.ArrayList;

/**
 * 地图上添加覆盖物
 */
public class OverlayActivity extends AppCompatActivity {

    private MapView mapView;
    private BaiduMap baiduMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_overlay);
        initMapView();
//        addMarkOverlay();
//        addAnimationOverlay();
        addPolyOverlay();


    }

    /**
     * 初始化百度map
     */
    private void initMapView() {
        mapView = (MapView) this.findViewById(R.id.overlayMapView);
        baiduMap = mapView.getMap();
        baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        baiduMap.setMaxAndMinZoomLevel(6, 20);
        MapStatus.Builder builder = new MapStatus.Builder().zoom(18).target(new LatLng(22.569645, 113.884268));
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(builder.build());
        baiduMap.setMapStatus(mapStatusUpdate);
    }

    /**
     * 添加标记覆盖物
     *
     *
     *
     * LatLng llA = new LatLng(39.963175, 116.400244);
     * LatLng llB = new LatLng(39.942821, 116.369199);
     * LatLng llC = new LatLng(39.939723, 116.425541);
     * LatLng llD = new LatLng(39.906965, 116.401394);
     */
    private void addMarkOverlay() {
        BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.mipmap.icon_marka);
        OverlayOptions options = new MarkerOptions().icon(descriptor).position(new LatLng(22.569645, 113.884268));
        baiduMap.addOverlay(options);
    }

    /**
     * 添加标记覆盖物添加动画
     */
    private void addAnimationOverlay() {
        MarkerOptions options = new MarkerOptions();

        BitmapDescriptor bd1 = BitmapDescriptorFactory.fromResource(R.mipmap.icon_marka);
        BitmapDescriptor bd2 = BitmapDescriptorFactory.fromResource(R.mipmap.icon_markb);
        BitmapDescriptor bd3 = BitmapDescriptorFactory.fromResource(R.mipmap.icon_markc);
        BitmapDescriptor bd4 = BitmapDescriptorFactory.fromResource(R.mipmap.icon_markd);
        BitmapDescriptor bd5 = BitmapDescriptorFactory.fromResource(R.mipmap.icon_marke);
        BitmapDescriptor bd6 = BitmapDescriptorFactory.fromResource(R.mipmap.icon_markf);
        BitmapDescriptor bd7 = BitmapDescriptorFactory.fromResource(R.mipmap.icon_markg);

        ArrayList<BitmapDescriptor> arrayList = new ArrayList<>();
        arrayList.add(bd1);
        arrayList.add(bd2);
        arrayList.add(bd3);
        arrayList.add(bd4);
        arrayList.add(bd5);
        arrayList.add(bd6);
        arrayList.add(bd7);

        options.icons(arrayList);
        options.position(new LatLng(22.569645, 113.884268));
        options.period(20);
        baiduMap.addOverlay(options);


        //不使用的时候记住回收
//        bd5.recycle();

    }

    private void addPolyOverlay() {
        BitmapDescriptor custom1 = BitmapDescriptorFactory
                .fromResource(R.mipmap.icon_st);
        BitmapDescriptor custom2 = BitmapDescriptorFactory
                .fromResource(R.mipmap.icon_st);
        BitmapDescriptor custom3 = BitmapDescriptorFactory
                .fromResource(R.mipmap.icon_st);
        ArrayList<BitmapDescriptor> bitmapDescriptors = new ArrayList<>();
        bitmapDescriptors.add(custom1);



        PolylineOptions options = new PolylineOptions();
        options.color(Color.RED);

        LatLng llA = new LatLng(39.963175, 116.400244);
        LatLng llB = new LatLng(39.942821, 116.369199);
        LatLng llC = new LatLng(39.939723, 116.425541);
        LatLng llD = new LatLng(39.906965, 116.401394);
        ArrayList<LatLng> latLngs = new ArrayList<>();
        ArrayList<Integer> arrayList = new ArrayList<>();

        latLngs.add(llA);
        arrayList.add(0);
        latLngs.add(llB);
        arrayList.add(0);
        latLngs.add(llC);
        arrayList.add(0);
        latLngs.add(llD);
        arrayList.add(0);
        options.points(latLngs);
        options.width(50);
        options.customTextureList(bitmapDescriptors);
        options.textureIndex(arrayList);
        baiduMap.addOverlay(options);
    }


    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
}
